<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2018/10/15

 * Time: 15:15

 */



namespace  app\mobile\controller;

use think\Db;

use think\Controller;





class Ppsj extends Bases

{



    public function _initialize(){

        if(!session('id')){

            $this->redirect('/admin/index/index');

        }

    }



    //客照列表

    public function index(){



        header("Cache-control: private");

        $is_recom=input('param.is_recom');

        $cg_id=intval(input('param.cg_id'));

        $venue_id=intval(input('param.venue_id'));

        $style_id=intval(input('param.style_id'));

        //推荐状态搜索

        if(isset($is_recom) && $is_recom !==''){

            $where['a.is_recom'] = $is_recom;

        }

        if(isset($cg_id) && $cg_id>0){

            $where['a.cg_id'] = $cg_id;

        }

        if(isset($venue_id) && $venue_id>0){

            $where['a.venue_id'] = $venue_id;

        }

        if(isset($style_id) && $style_id >0){

            $where['a.style_id'] = $style_id;

        }

       $where['a.is_del'] = 0;

        $where['a.sign'] = "mobile";
        
        $where['a.type'] = "4";

       $data = Db::name('zp')->alias('a')

           ->join('jd b','a.cg_id=b.id','left')

           ->join('style c','a.style_id=c.id','left')

           ->join('venue d','a.venue_id=d.id','left')

           ->where($where)

           ->order('a.sort desc,a.id desc')

           ->field('a.*,b.name cg_name,c.name style_name,d.name venue_name')

           ->select();



        //风格

        $data_style = Db::name('style')->where('is_del=0 and is_show=1 and  sign="mobile" and type="3"')->select();

        $this->assign('data_style',$data_style);

        $this->assign('data',$data);

        return $this->fetch();

    }

    //按钮状态

    public function start(){

//        $is_recom = (int)input('post.is_recom');

        $id = (int)input('post.id');

        if($id){

            $info=Db::name('zp')->where('id',$id)->find();

                if ($info['is_recom']==1){

                    $data['is_recom']=0;

                }else{

                    $data['is_recom']=1;

                }

            $result = Db::name('zp')->where('id',$id)->update($data);

            if($result){

                return json(['code'=>1,'msg'=>'操作成功']);

            }else{

                return json(['code'=>0,'msg'=>'操作失败']);

            }

        }else{

            return json(['code'=>1,'msg'=>'数据异常']);

        }

    }

    //添加风格样片

    public function add_zp(){

        $data_cg = Db::name('jd')->where('is_del=0 and is_show=1 and sign="mobile"')->select();

        $this->assign('data_cg',$data_cg);

        $data_style = Db::name('style')->where('is_del=0 and is_show=1 and sign="mobile" and type = "3"')->select();

        $this->assign('data_style',$data_style);

        //场馆

        $data_venue = Db::name('venue')->where('is_del=0 and is_show=1 and sign="mobile"')->order('sort desc,id desc')->select();

        $this->assign('data_venue',$data_venue);

        return $this->fetch();

    }

    //编辑

    public function edit_zp(){

        $id = input('param.id');

        if($id){

            $data =Db::name('zp')->where('id',$id)->find();

            $works_pic = Db('works_pic')->where('pic_id','in',$data['works_photo'])->field('works_pic,pic_id')->select();

            $data['content'] = html_entity_decode($data['content']);

            $this->assign('data',$data);

            $this->assign('imgurl',$works_pic);

        }

        $data_cg = Db::name('jd')->where('is_del=0 and is_show=1 and sign="mobile"')->select();

        $this->assign('data_cg',$data_cg);

        $data_style = Db::name('style')->where('is_del=0 and is_show=1 and sign="mobile" and type ="3"')->select();

        $this->assign('data_style',$data_style);

        //场馆

        $data_venue = Db::name('venue')->where('is_del=0 and is_show=1 and sign="mobile"')->order('sort desc,id desc')->select();

        $this->assign('data_venue',$data_venue);

        return $this->fetch();

    }

    //确认提交

    public function addzp(){

        $id = input('post.id');

        $array_works_pic= isset($_POST['works_pic'])?implode(',',$_POST['works_pic']):'';

        $data=[

            'title'=>input('post.title'),

            'ps_time'=>strtotime(input('post.ps_time')),

            'is_show'=>intval(input('post.is_show')),

            'is_recom'=>intval(input('post.is_recom')),

            'style_id'=>63,

            'cg_id'=>intval(input('post.cg_id')),

            'venue_id'=>intval(input('post.venue_id')),

            'sort'=>intval(input('post.sort')),

            'works_photo'=>$array_works_pic,

            'content'=>input('post.content'),

            'click'=>intval(input('post.click')),

            'seo_title'=>input('param.seo_title'),
            'count_r'=>input('param.count_r'),

            "type"=>"4",
            
            "sign"=>"mobile"

        ];

        $file_main_img = request()->file('main_img');

        if($file_main_img){

            $file_info_main_img = $file_main_img->move('./uploads/zp/');

            if($file_info_main_img){

                $data['main_img'] = '/uploads/zp/'.$file_info_main_img->getSaveName();

            }else{

                return json(['code'=>0,'msg'=>$file_main_img->getError()]);

            }

        }



        $file_seo_description = request()->file('seo_description');

        if($file_seo_description){

            $file_info_main_img = $file_seo_description->move('./uploads/zp/');

            if($file_info_main_img){

                $data['seo_description'] = '/uploads/zp/'.$file_info_main_img->getSaveName();

            }else{

                return json(['code'=>0,'msg'=>$file_seo_description->getError()]);

            }

        }

        if($id){

            if($file_main_img){

                $old_img = Db::name('zp')->where('id',$id)->value('main_img');

                if($old_img){



                    unlink('.'.str_replace('\\','/',$old_img));

                }

            }

            if($file_seo_description){

                $old_img = Db::name('zp')->where('id',$id)->value('seo_description');

                if($old_img){

                    unlink('.'.str_replace('\\','/',$old_img));

                }

            }



            $data['update_time']=time();

            $result = DB::name('zp')->where('id',$id)->update($data);

        }else{

            $data['create_time']=time();

            $result = Db::name('zp')->insert($data);

        }

        if($result !==false){

            return json(['code'=>1,'msg'=>'操作成功']);

        }else{

            return json(['code'=>0,'msg'=>'操作失败']);

        }

    }

    //删除

    public function zpdel(){

        $id = intval(input('post.id'));

        if($id){

            $res = Db::name('zp')->where('id',$id)->update(['is_del'=>1]);;

        }

        if($res){

            return json(['code'=>1,'msg'=>'操作成功']);

        }else{

            return json(['code'=>0,'msg'=>'操作失败']);

        }

    }

    //改变排序

    public function ChangeSort(){

        $sort = (int)input('post.sort');

        $id = (int)input('post.id');

        if($id){

            $data['sort']=$sort;

            $result = Db::name('zp')->where('id',$id)->update($data);

            if($result){

                return json(['code'=>1,'msg'=>'操作成功']);

            }else{

                return json(['code'=>0,'msg'=>'操作失败']);

            }

        }else{

            return json(['code'=>1,'msg'=>'数据异常']);

        }



    }



    //多图上传

    public function addworksajax(){

        $work_pic = request()->file('file');

        if(isset($work_pic)){

            $work_pic_file_info = $work_pic->move('./uploads/works/');

            if ($work_pic_file_info) {

                $data['works_pic'] = '/uploads/works/'.$work_pic_file_info->getSaveName();

                if($data){

                    $result = Db::name("works_pic")->insert($data);

                    $id = Db::name('works_pic')->getLastInsID();

                    $re = [

                        'status' => 200,

                        'info' => $id ,

                        'fileName'=>$data['works_pic'],

                    ];

                    if($result){

                        return json($re);

                    }

                }

            }

        }

    }

    //移除多图

    public function delworksajax(){

        $id = intval(input('param.id'));

        if($id>0){

            $url = Db::name('works_pic')

                ->where('pic_id',$id)

                ->value('works_pic');

            if($url){



                unlink(str_replace('\\','/',$_SERVER['DOCUMENT_ROOT'].$url));



            }

            $result = Db::name("works_pic")

                ->where('pic_id',$id)

                ->delete();

            if($result){

                return json(['status' => 200, 'info' => '移除成功']);

            }else{

                return  json(['status' => 400, 'info' => '移除失败']);

            }

        }else{

            return json(['status' => 400, 'info' => '数据异常']);

        }

    }



    public function jd(){

        $where['is_del']=0;

        $where['sign']="pc";

        $data = Db::name('jd')->where($where)->order('sort desc,id desc')->select();

        $this->assign('data',$data);



        return $this->fetch();

    }

    public function add_jd(){

        $id= intval(input('param.id'));

        if(request()->isAjax()){

            $data=[

                'name'=>input('param.name'),

                'is_show'=>intval(input('param.is_show')),

                'sort'=>intval(input('param.sort')),

                'sign'=>"mobile"

            ];

            $id= intval(input('param.id'));

            if($id> 0){

                $data['update_time']=time();

                $result = Db::name('jd')->where('id',$id)->update($data);

            }else{

                $data['create_time']=time();

                $result = Db::name('jd')->insert($data);

            }

            if($result !==false){

                return json(['code'=>1,'msg'=>'操作成功']);

            }else{

                return json(['code'=>0,'msg'=>'操作失败']);

            }



        }

        if($id){

            $data = Db::name('jd')->where('id',$id)->find();

            $this->assign('data',$data);

        }

        return $this->fetch();

    }



    //删除

    public function jddel(){

        $id = intval(input('post.id'));

        if($id){

            $check =Db::name('zp')->where('cg_id',$id)->select();

            if($check){

                return json(['code'=>0,'msg'=>'删除失败,错误信息:栏目已绑定']);

            }

            $res = Db::name('jd')->where('id',$id)->update(['is_del'=>1]);;

        }

        if($res){

            return json(['code'=>1,'msg'=>'操作成功']);

        }else{

            return json(['code'=>0,'msg'=>'操作失败']);

        }

    }

    //改变排序

    public function ChangeSortjd(){

        $sort = (int)input('post.sort');

        $id = (int)input('post.id');

        if($id){

            $data['sort']=$sort;

            $result = Db::name('jd')->where('id',$id)->update($data);

            if($result){

                return json(['code'=>1,'msg'=>'操作成功']);

            }else{

                return json(['code'=>0,'msg'=>'操作失败']);

            }

        }else{

            return json(['code'=>1,'msg'=>'数据异常']);

        }



    }

    //风格

    public function style(){

        $where['is_del']=0;

        $where['sign']="mobile";
        
        $where['type']="3";

        $data = Db::name('style')->where($where)->order('sort desc,id desc')->select();

        $this->assign('data',$data);

        return $this->fetch();

    }
    
    
    
    
    
    
    
    
    
    
    
      //添加样片风格
    
        public function add_style(){

        $id= intval(input('param.id'));



        if(request()->isAjax()){

            $data=[

                'name'=>input('param.name'),

                'is_show'=>intval(input('param.is_show')),

                'sort'=>intval(input('param.sort')),

                'sign'=>"mobile",
                
                 'type'=>"3"        //风格样片
                

            ];

            $id= intval(input('param.id'));

            if($id> 0){

                $data['update_time']=time();

                $result = Db::name('style')->where('id',$id)->update($data);

            }else{

                $data['create_time']=time();

                $result = Db::name('style')->insert($data);

            }

            if($result !==false){

                return json(['code'=>1,'msg'=>'操作成功']);

            }else{

                return json(['code'=>0,'msg'=>'操作失败']);

            }



        }

        if($id){

            $data = Db::name('style')->where('id',$id)->find();

            $this->assign('data',$data);

        }



        return $this->fetch();

    }
    
    
    
    
    
    
    
    
    
    


    public function styledel(){

        $id = intval(input('post.id'));

        if($id){

            $check =Db::name('zp')->where('style_id',$id)->select();

            if($check){

                return json(['code'=>0,'msg'=>'删除失败,错误信息:栏目已绑定']);

            }

            $res = Db::name('style')->where('id',$id)->delete();;

        }

        if($res){

            return json(['code'=>1,'msg'=>'操作成功']);

        }else{

            return json(['code'=>0,'msg'=>'操作失败']);

        }

    }



    //改变排序

    public function ChangeSortstyle()

    {

        $sort = (int)input('post.sort');

        $id = (int)input('post.id');

        if ($id) {

            $data['sort'] = $sort;

            $result = Db::name('style')->where('id', $id)->update($data);

            if ($result) {

                return json(['code' => 1, 'msg' => '操作成功']);

            } else {

                return json(['code' => 0, 'msg' => '操作失败']);

            }

        } else {

            return json(['code' => 1, 'msg' => '数据异常']);

        }

    }



    public function venue(){



        $where['is_del']=0;

        $where['sign']="mobile";

        $data = Db::name('venue')->where($where)->order('sort desc,id desc')->select();

        $this->assign('data',$data);

        return $this->fetch();

    }





    public function add_venue(){

        $id= intval(input('param.id'));



        if(request()->isAjax()){

            $data=[

                'name'=>input('param.name'),

                'is_show'=>intval(input('param.is_show')),

                'sort'=>intval(input('param.sort')),

                'sign'=>"mobile"

            ];

            $id= intval(input('param.id'));

            if($id> 0){

                $data['update_time']=time();

                $result = Db::name('venue')->where('id',$id)->update($data);

            }else{

                $data['create_time']=time();

                $result = Db::name('venue')->insert($data);

            }

            if($result !==false){

                return json(['code'=>1,'msg'=>'操作成功']);

            }else{

                return json(['code'=>0,'msg'=>'操作失败']);

            }



        }

        if($id){

            $data = Db::name('venue')->where('id',$id)->find();

            $this->assign('data',$data);

        }



        return $this->fetch();

    }





    public function venuedel(){

        $id = intval(input('post.id'));

        if($id){

            $check =Db::name('zp')->where('venue_id',$id)->select();

            if($check){

                return json(['code'=>0,'msg'=>'删除失败,错误信息:栏目已绑定']);

            }

            $res = Db::name('venue')->where('id',$id)->delete();;

        }

        if($res){

            return json(['code'=>1,'msg'=>'操作成功']);

        }else{

            return json(['code'=>0,'msg'=>'操作失败']);

        }

    }



    //改变排序

    public function ChangeSortvenue()

    {

        $sort = (int)input('post.sort');

        $id = (int)input('post.id');

        if ($id) {

            $data['sort'] = $sort;

            $result = Db::name('venue')->where('id', $id)->update($data);

            if ($result) {

                return json(['code' => 1, 'msg' => '操作成功']);

            } else {

                return json(['code' => 0, 'msg' => '操作失败']);

            }

        } else {

            return json(['code' => 1, 'msg' => '数据异常']);

        }

    }







}